package cn.tedu.cinema.movie.service.impl;

import cn.tedu.cinema.movie.mapper.MovieMapper;
import cn.tedu.cinema.movie.pojo.dto.MovieQuery;
import cn.tedu.cinema.movie.pojo.dto.MovieSaveParam;
import cn.tedu.cinema.movie.pojo.entity.Movie;
import cn.tedu.cinema.movie.pojo.vo.MovieVO;
import cn.tedu.cinema.movie.service.MovieService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieMapper movieMapper;


    @Override
    public void saveMovie(MovieSaveParam movieSaveParam) {
        log.debug("保存影片,参数={}", movieSaveParam);
        Movie movie = new Movie();
        BeanUtils.copyProperties(movieSaveParam, movie);
        if(movie.getId() == null){
            movie.setCreateTime(new Date());
            movieMapper.insertMovie(movie);
        }else{
            movie.setUpdateTime(new Date());
            movieMapper.updateMovie(movie);
        }
    }

    @Override
    public void deleteMovieById(Long id) {
        log.debug("删除影片业务,参数{}", id);
        movieMapper.deleteMovieById(id);
    }

    @Override
    public PageInfo<MovieVO> selectMovie(MovieQuery movieQuery) {
        log.debug("查询影片业务,参数{}", movieQuery);
        if(movieQuery.getPageNum() !=null && movieQuery.getPageSize()!=null){
            PageHelper.startPage(movieQuery.getPageNum(), movieQuery.getPageSize());
        }
        List<MovieVO> list = movieMapper.selectMovie(movieQuery);
        log.debug("查询结果:{}", movieMapper.selectMovie(movieQuery));
        PageInfo<MovieVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public MovieVO selectMovieById(Long id) {
        log.debug("查询影片业务,参数{}", id);
        MovieVO movieVO = movieMapper.selectMovieById(id);
        return movieVO;
    }


}
